/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javafxjsp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Rakesh Menon
 */
public class JavaFXTag extends SimpleTagSupport {

    private String version;
    private String archive;
    private String codebase;
    private boolean draggable;
    private int width;
    private int height;
    private String code;
    private String name;
    private String loading_image_url;
    private int loading_image_width;
    private int loading_image_height;
    private String args;

    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {

        JspWriter out = getJspContext().getOut();

        try {

            out.println();
            out.println("<!-- JavaFX Applet Tag generated by JavaFX Tag Library -->");
            out.println("<script src=\"http://dl.javafx.com/" + version + "/dtfx.js\"></script>");
            out.println("<script>");
            out.println("    javafx(");
            out.println("        {");
            out.println("            codebase: \"" + codebase + "\",");
            out.println("            archive: \"" + archive + "\",");
            out.println("            draggable: " + draggable + ",");
            out.println("            width: " + width + ",");
            out.println("            height: " + height + ",");
            out.println("            code: \"" + code + "\",");
            out.println("            name: \"" + name + "\",");

            if(loading_image_url != null) {
                out.println("            loading_image_url: \"" + loading_image_url + "\",");
                out.println("            loading_image_width: \"" + loading_image_width + "\",");
                out.println("            loading_image_height: \"" + loading_image_height + "\",");
            }

            out.println("        } , {");

            // Get the User-Agent header and pass it as argument to Applet
            PageContext pageContext = (PageContext) getJspContext();
            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            out.println("            UserAgent: \"" + request.getHeader("User-Agent") + "\",");

            if(args != null) {
                String[] keyVal = args.split(",");
                for(String arg : keyVal) {
                    String[] kv = arg.trim().split("=");
                    out.println("            " + kv[0] + ": \"" + kv[1] + "\",");
                }
            }

            out.println("        }");
            out.println("    );");
            out.println("</script>");
            out.println();

        } catch (java.io.IOException ex) {
            throw new JspException("Error in JavaFXTag tag", ex);
        }
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setArchive(String archive) {
        this.archive = archive;
    }

    public void setCodebase(String codebase) {
        this.codebase = codebase;
    }

    public void setDraggable(boolean draggable) {
        this.draggable = draggable;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLoading_image_url(String loading_image_url) {
        this.loading_image_url = loading_image_url;
    }

    public void setLoading_image_width(int loading_image_width) {
        this.loading_image_width = loading_image_width;
    }

    public void setLoading_image_height(int loading_image_height) {
        this.loading_image_height = loading_image_height;
    }

    public void setArgs(String args) {
        this.args = args;
    }
}
